package jp.co.ikd.util;

import static jp.co.ikd.util.DateUtil.addDay;
import static jp.co.ikd.util.DateUtil.addMonth;
import static jp.co.ikd.util.DateUtil.addYear;
import static jp.co.ikd.util.DateUtil.format;
import static jp.co.ikd.util.DateUtil.getLastDate;
import static jp.co.ikd.util.DateUtil.getLastDay;
import static jp.co.ikd.util.DateUtil.subtractDay;
import static jp.co.ikd.util.DateUtil.subtractMonth;
import static jp.co.ikd.util.DateUtil.subtractYear;
import static jp.co.ikd.util.DateUtil.toDate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

public class DateUtilTest {

	@Test
	public void test(){
		assertEquals(null, System.getProperty("a"));

		// -- Exception --
//		assertEquals(null, System.getProperty(""));
//		assertEquals(null, System.getProperty(null));
//		assertEquals("", System.getProperty(null,""));
	}

	@Test
	public void testGetDate(){
//		Integer.parseInt("a");
		System.out.println(DateUtil.getDate(2000, 1, 1, 1, 2, 3));
//		System.out.println(DateUtil.getDate(2000, 13, 1, 1, 2, 3));
//		System.out.println(DateUtil.getDate(0, 0, 0, 0, 0, 0));
//		System.out.println(DateUtil.getDate(2000, 0, 0, 0, 0, 0));
//		System.out.println(DateUtil.getDate(2000, 1, 0, 0, 0, 0));
		System.out.println(DateUtil.getDate(2000, 1, 1, 0, 0, 0));
		System.out.println(DateUtil.getDate(2000, 1, 1, 0, 0, 0));
//		System.out.println(DateUtil.getDate(0, 1, 1, 0, 0, 0));
		System.out.println(DateUtil.getDate(1, 1, 1, 0, 0, 0));
	}

	@Test
	public void testAddYear() throws Exception {

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date date = df.parse("2001/01/01");

		assertEquals("2002/01/01", df.format(addYear(date, 1)));
		assertEquals("2001/01/01", df.format(addYear(date, 0)));
		assertEquals("2000/01/01", df.format(addYear(date, -1)));

		assertEquals("2002/02/02", df.format(addDay(addMonth(addYear(date, 1), 1), 1)));
		// TODO: add続きはラップしないと...
		// assertEquals("2002/01/01", df.format(addYear(date, 1).addMonth()
		// .addDay());

		try {
			df.format(addYear(null, 1));
			fail();
		} catch (NullPointerException ne) {
		} catch (Exception e) {
			fail();
		}
	}

	@Test
	public void testAddMonth() throws Exception {

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date date = df.parse("2001/01/01");

		assertEquals("2001/02/01", df.format(addMonth(date, 1)));
	}

	@Test
	public void testAddDay() throws Exception {

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date date = df.parse("2001/01/01");

		assertEquals("2001/01/02", df.format(addDay(date, 1)));
	}

	@Test
	public void testSubtractYear() throws Exception {

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date date = df.parse("2001/01/01");

		assertEquals("2000/01/01", df.format(subtractYear(date, 1)));
	}

	@Test
	public void testSubtractMonth() throws Exception {

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date date = df.parse("2001/01/01");

		assertEquals("2000/12/01", df.format(subtractMonth(date, 1)));
	}

	@Test
	public void testSubtractDay() throws Exception {

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date date = df.parse("2001/01/01");

		assertEquals("2000/12/31", df.format(subtractDay(date, 1)));
	}

	@Test
	public void getLastDay_int() throws Exception {

		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Date date = df.parse("20010101");

		assertEquals(31, getLastDay(date));

		assertEquals("うるう年の2月", 29, getLastDay(df.parse("20080201")));
		assertEquals("うるう年の2月", 29, getLastDay(df.parse("20120201")));
	}
	
	@Test
	public void testGetLastDate() throws Exception{
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Date date = df.parse("20010101");
		
		assertEquals(df.parse("20010131"), getLastDate(date));
	}

}
